using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using T4MVCGen.Core;
using T4MVCGen;

namespace DataTierGenerator
{
	/// <summary>
	/// Form used to collect the connection information for the code we're going to generate.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label serverLabel;
		private System.Windows.Forms.Label databaseLabel;
		private System.Windows.Forms.GroupBox authenticationGroupBox;
		private System.Windows.Forms.TextBox passwordTextBox;
		private System.Windows.Forms.TextBox loginNameTextBox;
		private System.Windows.Forms.Label passwordLabel;
		private System.Windows.Forms.Label loginNameLabel;
		private System.Windows.Forms.RadioButton sqlServerAuthenticationRadioButton;
		private System.Windows.Forms.RadioButton windowsAuthenticationRadioButton;
		private System.Windows.Forms.TextBox serverTextBox;
		private System.Windows.Forms.TextBox databaseTextBox;
        private System.Windows.Forms.Button generateButton;
		private System.Windows.Forms.CheckBox multipleFilesCheckBox;
		private System.Windows.Forms.GroupBox csGroupBox;
		private System.Windows.Forms.TextBox namespaceTextBox;
        private System.Windows.Forms.Label namespaceLabel;
		private System.Windows.Forms.ProgressBar progressBar;
		private System.Windows.Forms.TextBox daoSuffixTextBox;
		private System.Windows.Forms.Label daoSuffixLabel;
        private TextBox projectTextBox;
        private Label projectLabel;
        private TextBox dtoSuffixTextBox;
        private Label dtoSuffixLabel;
        private GroupBox tablesGroupBox;
        private Button btnConnect;
        private GroupBox grpDatabase;
        private CheckBox chkTblSelectAll;
        private CheckedListBox chkSelectTables;
        private ComboBox ddnDbType;
        private Label lblDbType;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		
		public MainForm()
	    {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

            System.Collections.Generic.IDictionary<int, string> dict = Utility.EnumToDict(typeof(DatabaseType));
            ddnDbType.DataSource = new BindingSource(dict, null);
            ddnDbType.DisplayMember = "Value";
            ddnDbType.ValueMember = "Key";

            T4Generator.DatabaseCounted += new T4MVCGen.Core.CountUpdate(Generator_DatabaseCountedT4);
            T4Generator.TableCounted += new T4MVCGen.Core.CountUpdate(Generator_TableCountedT4);
			// Load settings from the Registry
            EnvSettings settings = new EnvSettings();
            settings.LoadSettings();
            serverTextBox.Text = settings.Server;
            databaseTextBox.Text = settings.Database;
				if (settings.AuthenticationType == 1)
	            {
					windowsAuthenticationRadioButton.Checked = true;
				}
                else
	            {
					sqlServerAuthenticationRadioButton.Checked = true;
				}

                loginNameTextBox.Text = settings.Login;
				if (settings.CreateMultipleFiles == 1)
	            {
					multipleFilesCheckBox.Checked = true;
				}
                ddnDbType.SelectedValue = settings.DbType;

                projectTextBox.Text = settings.Project;
                namespaceTextBox.Text = settings.Namespace;
                daoSuffixTextBox.Text = settings.DaoSuffix;
                dtoSuffixTextBox.Text = settings.DtoSuffix;
		}
		
		private void DatabaseTextBox_TextChanged(object sender, System.EventArgs e)
	    {
			EnableGenerateButton();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
	    {
			if (disposing)
	        {
				if (components != null)
	            {
					components.Dispose();
				}
			}

			base.Dispose(disposing);
		}

		private void EnableGenerateButton()
	    {
			if (serverTextBox.Text.Length == 0)
	        {
				generateButton.Enabled = false;
				return;
			}

			if (databaseTextBox.Text.Length == 0)
	        {
				generateButton.Enabled = false;
				return;
			}
		
			if (sqlServerAuthenticationRadioButton.Checked)
	        {
				if (loginNameTextBox.Text.Length == 0)
	            {
					generateButton.Enabled = false;
					return;
				}
			}

			if (namespaceTextBox.Text.Length == 0)
	        {
				generateButton.Enabled = false;
				return;
			}

			generateButton.Enabled = true;
		}

		private void GenerateButton_Click(object sender, System.EventArgs e)
    	{
			string connectionString;
			
			try
	        {
				generateButton.Enabled = false;
				
				// Build the connection string
				if (windowsAuthenticationRadioButton.Checked)
	            {
					connectionString = "Server=" + serverTextBox.Text + "; Database=" + databaseTextBox.Text + "; Integrated Security=SSPI;";
				}
                else
	            {
					connectionString = "Server=" + serverTextBox.Text + "; Database=" + databaseTextBox.Text + "; User ID=" + loginNameTextBox.Text + "; Password=" + passwordTextBox.Text + ";";
				}
				
				// Let the user select where to create the C# and SQL code
                string outputDirectory;
                EnvSettings settings = new EnvSettings();
                settings.LoadSettings();
                outputDirectory = settings.OutputDirectory;

                string[] selectedTables = new string[chkSelectTables.CheckedItems.Count];
                if (chkSelectTables.CheckedItems.Count > 0)
                {
                    int count = 0;
                    foreach (string item in chkSelectTables.CheckedItems)
                    {
                        selectedTables[count] = item;
                        count++;
                    }
                }
                else
                {
                    throw new Exception("No tables selected");
                }

				using (FolderBrowserDialog dialog = new FolderBrowserDialog())
	            {
					dialog.Description = "Select an output directory";
                    dialog.SelectedPath = outputDirectory;
					dialog.ShowNewFolderButton = true;
					if (dialog.ShowDialog(this) == DialogResult.OK)
	                {
						outputDirectory = dialog.SelectedPath;
					}
                    else
	                {
						return;
					}
				}



                Options options = new Options(outputDirectory, (int)ddnDbType.SelectedValue, connectionString,selectedTables, multipleFilesCheckBox.Checked, projectTextBox.Text, namespaceTextBox.Text, daoSuffixTextBox.Text, dtoSuffixTextBox.Text);
				// Generate the SQL and C# code
                
                T4Generator.Generate(options);

                settings.Server = serverTextBox.Text;
                settings.Database = databaseTextBox.Text;
                if (windowsAuthenticationRadioButton.Checked)
                {
                    settings.AuthenticationType=1;
                }
                else
                {
                    settings.AuthenticationType=2;
                }

                settings.Login= loginNameTextBox.Text;
                if (multipleFilesCheckBox.Checked)
                {
                    settings.CreateMultipleFiles= 1;
                }
                else
                {
                    settings.CreateMultipleFiles= 0;
                }
                settings.DbType = (int)ddnDbType.SelectedValue;
                settings.Project= projectTextBox.Text;
                settings.Namespace= namespaceTextBox.Text;
                settings.DaoSuffix= daoSuffixTextBox.Text;
                settings.DtoSuffix= dtoSuffixTextBox.Text;
                settings.OutputDirectory= outputDirectory;
                settings.SaveSettings();
				// Inform the user we're done
				MessageBox.Show("C# classes and stored procedures generated successfully.");
			}
            catch (Exception ex)
	        {
				MessageBox.Show(ex.Message);
			}
            finally
	        {
				progressBar.Value = 0;
				generateButton.Enabled = true;
			}
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
	    {
            this.serverLabel = new System.Windows.Forms.Label();
            this.databaseLabel = new System.Windows.Forms.Label();
            this.authenticationGroupBox = new System.Windows.Forms.GroupBox();
            this.passwordTextBox = new System.Windows.Forms.TextBox();
            this.loginNameTextBox = new System.Windows.Forms.TextBox();
            this.passwordLabel = new System.Windows.Forms.Label();
            this.loginNameLabel = new System.Windows.Forms.Label();
            this.sqlServerAuthenticationRadioButton = new System.Windows.Forms.RadioButton();
            this.windowsAuthenticationRadioButton = new System.Windows.Forms.RadioButton();
            this.serverTextBox = new System.Windows.Forms.TextBox();
            this.databaseTextBox = new System.Windows.Forms.TextBox();
            this.generateButton = new System.Windows.Forms.Button();
            this.multipleFilesCheckBox = new System.Windows.Forms.CheckBox();
            this.csGroupBox = new System.Windows.Forms.GroupBox();
            this.projectTextBox = new System.Windows.Forms.TextBox();
            this.projectLabel = new System.Windows.Forms.Label();
            this.dtoSuffixTextBox = new System.Windows.Forms.TextBox();
            this.dtoSuffixLabel = new System.Windows.Forms.Label();
            this.daoSuffixTextBox = new System.Windows.Forms.TextBox();
            this.daoSuffixLabel = new System.Windows.Forms.Label();
            this.namespaceTextBox = new System.Windows.Forms.TextBox();
            this.namespaceLabel = new System.Windows.Forms.Label();
            this.progressBar = new System.Windows.Forms.ProgressBar();
            this.tablesGroupBox = new System.Windows.Forms.GroupBox();
            this.btnConnect = new System.Windows.Forms.Button();
            this.grpDatabase = new System.Windows.Forms.GroupBox();
            this.chkTblSelectAll = new System.Windows.Forms.CheckBox();
            this.chkSelectTables = new System.Windows.Forms.CheckedListBox();
            this.lblDbType = new System.Windows.Forms.Label();
            this.ddnDbType = new System.Windows.Forms.ComboBox();
            this.authenticationGroupBox.SuspendLayout();
            this.csGroupBox.SuspendLayout();
            this.tablesGroupBox.SuspendLayout();
            this.grpDatabase.SuspendLayout();
            this.SuspendLayout();
            // 
            // serverLabel
            // 
            this.serverLabel.Location = new System.Drawing.Point(13, 68);
            this.serverLabel.Name = "serverLabel";
            this.serverLabel.Size = new System.Drawing.Size(60, 23);
            this.serverLabel.TabIndex = 0;
            this.serverLabel.Text = "Server:";
            // 
            // databaseLabel
            // 
            this.databaseLabel.Location = new System.Drawing.Point(13, 91);
            this.databaseLabel.Name = "databaseLabel";
            this.databaseLabel.Size = new System.Drawing.Size(60, 23);
            this.databaseLabel.TabIndex = 1;
            this.databaseLabel.Text = "Database:";
            // 
            // authenticationGroupBox
            // 
            this.authenticationGroupBox.Controls.Add(this.passwordTextBox);
            this.authenticationGroupBox.Controls.Add(this.loginNameTextBox);
            this.authenticationGroupBox.Controls.Add(this.passwordLabel);
            this.authenticationGroupBox.Controls.Add(this.loginNameLabel);
            this.authenticationGroupBox.Controls.Add(this.sqlServerAuthenticationRadioButton);
            this.authenticationGroupBox.Controls.Add(this.windowsAuthenticationRadioButton);
            this.authenticationGroupBox.Location = new System.Drawing.Point(16, 131);
            this.authenticationGroupBox.Name = "authenticationGroupBox";
            this.authenticationGroupBox.Size = new System.Drawing.Size(240, 138);
            this.authenticationGroupBox.TabIndex = 2;
            this.authenticationGroupBox.TabStop = false;
            this.authenticationGroupBox.Text = "Authentication";
            // 
            // passwordTextBox
            // 
            this.passwordTextBox.BackColor = System.Drawing.SystemColors.InactiveBorder;
            this.passwordTextBox.Enabled = false;
            this.passwordTextBox.Location = new System.Drawing.Point(102, 108);
            this.passwordTextBox.Name = "passwordTextBox";
            this.passwordTextBox.PasswordChar = '*';
            this.passwordTextBox.Size = new System.Drawing.Size(132, 20);
            this.passwordTextBox.TabIndex = 5;
            // 
            // loginNameTextBox
            // 
            this.loginNameTextBox.BackColor = System.Drawing.SystemColors.InactiveBorder;
            this.loginNameTextBox.Enabled = false;
            this.loginNameTextBox.Location = new System.Drawing.Point(102, 78);
            this.loginNameTextBox.Name = "loginNameTextBox";
            this.loginNameTextBox.Size = new System.Drawing.Size(132, 20);
            this.loginNameTextBox.TabIndex = 4;
            this.loginNameTextBox.TextChanged += new System.EventHandler(this.LoginNameTextBox_TextChanged);
            // 
            // passwordLabel
            // 
            this.passwordLabel.Enabled = false;
            this.passwordLabel.Location = new System.Drawing.Point(30, 108);
            this.passwordLabel.Name = "passwordLabel";
            this.passwordLabel.Size = new System.Drawing.Size(72, 23);
            this.passwordLabel.TabIndex = 3;
            this.passwordLabel.Text = "Password:";
            // 
            // loginNameLabel
            // 
            this.loginNameLabel.Enabled = false;
            this.loginNameLabel.Location = new System.Drawing.Point(30, 78);
            this.loginNameLabel.Name = "loginNameLabel";
            this.loginNameLabel.Size = new System.Drawing.Size(72, 23);
            this.loginNameLabel.TabIndex = 2;
            this.loginNameLabel.Text = "Login Name:";
            // 
            // sqlServerAuthenticationRadioButton
            // 
            this.sqlServerAuthenticationRadioButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.sqlServerAuthenticationRadioButton.Location = new System.Drawing.Point(12, 48);
            this.sqlServerAuthenticationRadioButton.Name = "sqlServerAuthenticationRadioButton";
            this.sqlServerAuthenticationRadioButton.Size = new System.Drawing.Size(156, 24);
            this.sqlServerAuthenticationRadioButton.TabIndex = 3;
            this.sqlServerAuthenticationRadioButton.Text = "SQL Server Authentication";
            this.sqlServerAuthenticationRadioButton.CheckedChanged += new System.EventHandler(this.SqlServerAuthenticationRadioButton_CheckedChanged);
            // 
            // windowsAuthenticationRadioButton
            // 
            this.windowsAuthenticationRadioButton.Checked = true;
            this.windowsAuthenticationRadioButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.windowsAuthenticationRadioButton.Location = new System.Drawing.Point(12, 24);
            this.windowsAuthenticationRadioButton.Name = "windowsAuthenticationRadioButton";
            this.windowsAuthenticationRadioButton.Size = new System.Drawing.Size(156, 24);
            this.windowsAuthenticationRadioButton.TabIndex = 2;
            this.windowsAuthenticationRadioButton.TabStop = true;
            this.windowsAuthenticationRadioButton.Text = "Windows Authentication";
            this.windowsAuthenticationRadioButton.CheckedChanged += new System.EventHandler(this.WindowsAuthenticationRadioButton_CheckedChanged);
            // 
            // serverTextBox
            // 
            this.serverTextBox.Location = new System.Drawing.Point(90, 65);
            this.serverTextBox.Name = "serverTextBox";
            this.serverTextBox.Size = new System.Drawing.Size(180, 20);
            this.serverTextBox.TabIndex = 0;
            this.serverTextBox.TextChanged += new System.EventHandler(this.ServerTextBox_TextChanged);
            // 
            // databaseTextBox
            // 
            this.databaseTextBox.Location = new System.Drawing.Point(90, 94);
            this.databaseTextBox.Name = "databaseTextBox";
            this.databaseTextBox.Size = new System.Drawing.Size(180, 20);
            this.databaseTextBox.TabIndex = 1;
            this.databaseTextBox.TextChanged += new System.EventHandler(this.DatabaseTextBox_TextChanged);
            // 
            // generateButton
            // 
            this.generateButton.Enabled = false;
            this.generateButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.generateButton.Location = new System.Drawing.Point(533, 540);
            this.generateButton.Name = "generateButton";
            this.generateButton.Size = new System.Drawing.Size(72, 24);
            this.generateButton.TabIndex = 10;
            this.generateButton.Text = "Generate";
            this.generateButton.Click += new System.EventHandler(this.GenerateButton_Click);
            // 
            // multipleFilesCheckBox
            // 
            this.multipleFilesCheckBox.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.multipleFilesCheckBox.Location = new System.Drawing.Point(15, 131);
            this.multipleFilesCheckBox.Name = "multipleFilesCheckBox";
            this.multipleFilesCheckBox.Size = new System.Drawing.Size(216, 24);
            this.multipleFilesCheckBox.TabIndex = 8;
            this.multipleFilesCheckBox.Text = "Create multiple files for stored procedures";
            // 
            // csGroupBox
            // 
            this.csGroupBox.Controls.Add(this.projectTextBox);
            this.csGroupBox.Controls.Add(this.projectLabel);
            this.csGroupBox.Controls.Add(this.dtoSuffixTextBox);
            this.csGroupBox.Controls.Add(this.dtoSuffixLabel);
            this.csGroupBox.Controls.Add(this.multipleFilesCheckBox);
            this.csGroupBox.Controls.Add(this.daoSuffixTextBox);
            this.csGroupBox.Controls.Add(this.daoSuffixLabel);
            this.csGroupBox.Controls.Add(this.namespaceTextBox);
            this.csGroupBox.Controls.Add(this.namespaceLabel);
            this.csGroupBox.Location = new System.Drawing.Point(15, 326);
            this.csGroupBox.Name = "csGroupBox";
            this.csGroupBox.Size = new System.Drawing.Size(288, 198);
            this.csGroupBox.TabIndex = 10;
            this.csGroupBox.TabStop = false;
            this.csGroupBox.Text = "C#";
            // 
            // projectTextBox
            // 
            this.projectTextBox.Location = new System.Drawing.Point(78, 20);
            this.projectTextBox.Name = "projectTextBox";
            this.projectTextBox.Size = new System.Drawing.Size(204, 20);
            this.projectTextBox.TabIndex = 10;
            // 
            // projectLabel
            // 
            this.projectLabel.Location = new System.Drawing.Point(12, 20);
            this.projectLabel.Name = "projectLabel";
            this.projectLabel.Size = new System.Drawing.Size(72, 23);
            this.projectLabel.TabIndex = 9;
            this.projectLabel.Text = "Project:";
            // 
            // dtoSuffixTextBox
            // 
            this.dtoSuffixTextBox.Location = new System.Drawing.Point(77, 106);
            this.dtoSuffixTextBox.Name = "dtoSuffixTextBox";
            this.dtoSuffixTextBox.Size = new System.Drawing.Size(204, 20);
            this.dtoSuffixTextBox.TabIndex = 16;
            // 
            // dtoSuffixLabel
            // 
            this.dtoSuffixLabel.Location = new System.Drawing.Point(11, 106);
            this.dtoSuffixLabel.Name = "dtoSuffixLabel";
            this.dtoSuffixLabel.Size = new System.Drawing.Size(72, 23);
            this.dtoSuffixLabel.TabIndex = 15;
            this.dtoSuffixLabel.Text = "DTO Suffix:";
            // 
            // daoSuffixTextBox
            // 
            this.daoSuffixTextBox.Location = new System.Drawing.Point(78, 77);
            this.daoSuffixTextBox.Name = "daoSuffixTextBox";
            this.daoSuffixTextBox.Size = new System.Drawing.Size(204, 20);
            this.daoSuffixTextBox.TabIndex = 14;
            // 
            // daoSuffixLabel
            // 
            this.daoSuffixLabel.Location = new System.Drawing.Point(12, 77);
            this.daoSuffixLabel.Name = "daoSuffixLabel";
            this.daoSuffixLabel.Size = new System.Drawing.Size(72, 23);
            this.daoSuffixLabel.TabIndex = 13;
            this.daoSuffixLabel.Text = "DAO Suffix:";
            // 
            // namespaceTextBox
            // 
            this.namespaceTextBox.Location = new System.Drawing.Point(78, 47);
            this.namespaceTextBox.Name = "namespaceTextBox";
            this.namespaceTextBox.Size = new System.Drawing.Size(204, 20);
            this.namespaceTextBox.TabIndex = 12;
            this.namespaceTextBox.TextChanged += new System.EventHandler(this.namespaceTextBox_TextChanged);
            // 
            // namespaceLabel
            // 
            this.namespaceLabel.Location = new System.Drawing.Point(12, 47);
            this.namespaceLabel.Name = "namespaceLabel";
            this.namespaceLabel.Size = new System.Drawing.Size(72, 23);
            this.namespaceLabel.TabIndex = 11;
            this.namespaceLabel.Text = "Namespace:";
            // 
            // progressBar
            // 
            this.progressBar.Location = new System.Drawing.Point(12, 541);
            this.progressBar.Name = "progressBar";
            this.progressBar.Size = new System.Drawing.Size(515, 23);
            this.progressBar.TabIndex = 11;
            // 
            // tablesGroupBox
            // 
            this.tablesGroupBox.Controls.Add(this.chkSelectTables);
            this.tablesGroupBox.Controls.Add(this.chkTblSelectAll);
            this.tablesGroupBox.Location = new System.Drawing.Point(290, 19);
            this.tablesGroupBox.Name = "tablesGroupBox";
            this.tablesGroupBox.Size = new System.Drawing.Size(309, 291);
            this.tablesGroupBox.TabIndex = 12;
            this.tablesGroupBox.TabStop = false;
            this.tablesGroupBox.Text = "Tables";
            // 
            // btnConnect
            // 
            this.btnConnect.Location = new System.Drawing.Point(195, 285);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(75, 23);
            this.btnConnect.TabIndex = 13;
            this.btnConnect.Text = "Connect";
            this.btnConnect.UseVisualStyleBackColor = true;
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            // 
            // grpDatabase
            // 
            this.grpDatabase.Controls.Add(this.ddnDbType);
            this.grpDatabase.Controls.Add(this.lblDbType);
            this.grpDatabase.Controls.Add(this.btnConnect);
            this.grpDatabase.Controls.Add(this.tablesGroupBox);
            this.grpDatabase.Controls.Add(this.serverTextBox);
            this.grpDatabase.Controls.Add(this.serverLabel);
            this.grpDatabase.Controls.Add(this.authenticationGroupBox);
            this.grpDatabase.Controls.Add(this.databaseLabel);
            this.grpDatabase.Controls.Add(this.databaseTextBox);
            this.grpDatabase.Location = new System.Drawing.Point(12, 12);
            this.grpDatabase.Name = "grpDatabase";
            this.grpDatabase.Size = new System.Drawing.Size(605, 316);
            this.grpDatabase.TabIndex = 14;
            this.grpDatabase.TabStop = false;
            this.grpDatabase.Text = "Database";
            // 
            // chkTblSelectAll
            // 
            this.chkTblSelectAll.AutoSize = true;
            this.chkTblSelectAll.Checked = true;
            this.chkTblSelectAll.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkTblSelectAll.Enabled = false;
            this.chkTblSelectAll.Location = new System.Drawing.Point(222, 268);
            this.chkTblSelectAll.Name = "chkTblSelectAll";
            this.chkTblSelectAll.Size = new System.Drawing.Size(70, 17);
            this.chkTblSelectAll.TabIndex = 14;
            this.chkTblSelectAll.Text = "Select All";
            this.chkTblSelectAll.UseVisualStyleBackColor = true;
            this.chkTblSelectAll.CheckedChanged += new System.EventHandler(this.chkTblSelectAll_CheckedChanged);
            // 
            // chkSelectTables
            // 
            this.chkSelectTables.Enabled = false;
            this.chkSelectTables.FormattingEnabled = true;
            this.chkSelectTables.Location = new System.Drawing.Point(6, 18);
            this.chkSelectTables.Name = "chkSelectTables";
            this.chkSelectTables.Size = new System.Drawing.Size(286, 244);
            this.chkSelectTables.TabIndex = 0;
            // 
            // lblDbType
            // 
            this.lblDbType.AutoSize = true;
            this.lblDbType.Location = new System.Drawing.Point(16, 37);
            this.lblDbType.Name = "lblDbType";
            this.lblDbType.Size = new System.Drawing.Size(51, 13);
            this.lblDbType.TabIndex = 14;
            this.lblDbType.Text = "Db Type:";
            // 
            // ddnDbType
            // 
            this.ddnDbType.FormattingEnabled = true;
            this.ddnDbType.Location = new System.Drawing.Point(90, 34);
            this.ddnDbType.Name = "ddnDbType";
            this.ddnDbType.Size = new System.Drawing.Size(121, 21);
            this.ddnDbType.TabIndex = 15;
            // 
            // MainForm
            // 
            this.AcceptButton = this.generateButton;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(621, 568);
            this.Controls.Add(this.grpDatabase);
            this.Controls.Add(this.progressBar);
            this.Controls.Add(this.csGroupBox);
            this.Controls.Add(this.generateButton);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "MainForm";
            this.Text = "Data Tier Generator";
            this.authenticationGroupBox.ResumeLayout(false);
            this.authenticationGroupBox.PerformLayout();
            this.csGroupBox.ResumeLayout(false);
            this.csGroupBox.PerformLayout();
            this.tablesGroupBox.ResumeLayout(false);
            this.tablesGroupBox.PerformLayout();
            this.grpDatabase.ResumeLayout(false);
            this.grpDatabase.PerformLayout();
            this.ResumeLayout(false);

		}

		private void LoginNameTextBox_TextChanged(object sender, System.EventArgs e)
	    {
			EnableGenerateButton();
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		public static void Main()
	    {
			Application.EnableVisualStyles();
			Application.DoEvents();
			Application.Run(new MainForm());
		}

		private void ServerTextBox_TextChanged(object sender, System.EventArgs e)
	    {
			EnableGenerateButton();
		}

		private void SqlServerAuthenticationRadioButton_CheckedChanged(object sender, System.EventArgs e)
	    {
			loginNameLabel.Enabled = true;
			loginNameTextBox.Enabled  = true;
			loginNameTextBox.BackColor = SystemColors.Window;

			passwordLabel.Enabled = true;
			passwordTextBox.Enabled  = true;
			passwordTextBox.BackColor = SystemColors.Window;

			EnableGenerateButton();
		}

		private void WindowsAuthenticationRadioButton_CheckedChanged(object sender, System.EventArgs e)
	    {
			loginNameLabel.Enabled = false;
			loginNameTextBox.Enabled  = false;
			loginNameTextBox.BackColor = SystemColors.InactiveBorder;
			loginNameTextBox.Text = "";

			passwordLabel.Enabled = false;
			passwordTextBox.Enabled  = false;
			passwordTextBox.BackColor = SystemColors.InactiveBorder;
			passwordTextBox.Text = "";

			EnableGenerateButton();
		}

		private void namespaceTextBox_TextChanged(object sender, System.EventArgs e)
	    {
			EnableGenerateButton();
		}

		private void Generator_DatabaseCounted(object sender, CountEventArgs e)
	    {
			progressBar.Maximum = e.Count;
		}

		private void Generator_TableCounted(object sender, CountEventArgs e)
	    {
			progressBar.Value = e.Count;
		}

        private void Generator_TableCountedT4(object sender, CountEventArgs e) { progressBar.Value = e.Count; }

        private void Generator_DatabaseCountedT4(object sender, CountEventArgs e) { progressBar.Maximum = e.Count; if ((e.TableNames != null)&&(e.TableNames.Length>0)) { chkSelectTables.Items.AddRange(e.TableNames); } }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            			string connectionString;

                        try
                        {
                            generateButton.Enabled = false;

                            // Build the connection string
                            if (windowsAuthenticationRadioButton.Checked)
                            {
                                connectionString = "Server=" + serverTextBox.Text + "; Database=" + databaseTextBox.Text + "; Integrated Security=SSPI;";
                            }
                            else
                            {
                                connectionString = "Server=" + serverTextBox.Text + "; Database=" + databaseTextBox.Text + "; User ID=" + loginNameTextBox.Text + "; Password=" + passwordTextBox.Text + ";";
                            }

                            Options options = new Options(connectionString,(int)ddnDbType.SelectedValue);

                            T4Generator.ConnectDatabase(options);

                            chkSelectTables.Enabled = true;
                            setAllChecked(chkSelectTables, true);
                            chkTblSelectAll.Enabled = true;
                            // Inform the user we're done
                            MessageBox.Show("Connection successfull.");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            progressBar.Value = 0;
                            generateButton.Enabled = true;
                        }
        }

        private void chkTblSelectAll_CheckedChanged(object sender, EventArgs e)
        {
                setAllChecked(chkSelectTables,((CheckBox)sender).Checked);
        }

        private void setAllChecked(CheckedListBox checkedListBox,bool isEnabled)
        {
            for (int tmp = 0; tmp < checkedListBox.Items.Count; tmp++)
            {
                checkedListBox.SetItemChecked(tmp, isEnabled);
            }
        }
    }
}
